package c5_messageSide;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

/**
 * @author： pang-yun
 * @date： 2021-09-08 15:34
 * 功能目的 ：用于 扩容 byteBuffer
 */

public class MessageSideServer {
    public static void main(String[] args) throws IOException {
        Selector selector = Selector.open();
        ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.configureBlocking(false);
        SelectionKey sscKey = ssc.register(selector, SelectionKey.OP_ACCEPT, null);
        ssc.bind(new InetSocketAddress(9999));

        while (true) {
            selector.select();

            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                iterator.remove();
                if (key.isAcceptable()) {
                    ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
                    SocketChannel socketChannel = serverSocketChannel.accept();
                    socketChannel.configureBlocking(false);
                    ByteBuffer byteBuffer = ByteBuffer.allocate(16);
                    socketChannel.register(selector, SelectionKey.OP_READ, byteBuffer);
                } else if (key.isReadable()) {
                    SocketChannel sc = (SocketChannel) key.channel();
                    ByteBuffer attachment = (ByteBuffer) key.attachment();
                    try {
                        int read = sc.read(attachment);
                        if (read == -1) {
                            key.cancel();
                        }
                        attachment.flip();
                        // 循环找到  '\n'
                        for (int i = 0; i < attachment.limit(); i++) {
                            if (attachment.get(i) == '\n') {
                                int length = i + 1 - attachment.position();
                                ByteBuffer newBuffer = ByteBuffer.allocate(length);
                                for (int j = 0; j < length; j++) {
                                    byte b = attachment.get();
                                    newBuffer.put(b);
                                }
                                // 读取内容
                                newBuffer.flip();
                                while (newBuffer.hasRemaining()){
                                    byte b = newBuffer.get();
                                    System.out.print((char) b);
                                }
                            }
                        }
                        attachment.compact();
                        
                        //遍历之后 查看是否容量不够
                        if (attachment.position()==attachment.limit()){
                            ByteBuffer dilatationByteBuffer = ByteBuffer.allocate(attachment.capacity() * 2);
                            dilatationByteBuffer.flip();
                            dilatationByteBuffer.put(attachment);
                            key.attach(dilatationByteBuffer);
                        }


                    } catch (IOException e) {
                        key.cancel();
                    }


                }
            }
        }
    }
}
